/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.property.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.property.domain.Money;
import me.zhengjie.modules.property.service.MoneyService;
import me.zhengjie.modules.property.service.dto.MoneyQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2022-07-02
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "money管理")
@RequestMapping("/api/money")
public class MoneyController {

    private final MoneyService moneyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('money:list')")
    public void download(HttpServletResponse response, MoneyQueryCriteria criteria) throws IOException {
        moneyService.download(moneyService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询money")
    @ApiOperation("查询money")
    @PreAuthorize("@el.check('money:list')")
    public ResponseEntity<Object> query(MoneyQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(moneyService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增money")
    @ApiOperation("新增money")
    @PreAuthorize("@el.check('money:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Money resources){
        return new ResponseEntity<>(moneyService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改money")
    @ApiOperation("修改money")
    @PreAuthorize("@el.check('money:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Money resources){
        moneyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除money")
    @ApiOperation("删除money")
    @PreAuthorize("@el.check('money:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        moneyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("查找最新的创建日期")
    @GetMapping(value = "/getNewCreateTime")
    @AnonymousAccess
    public Boolean getNewCreateTime() {
       return moneyService.getNewCreateTime();
    }

    /*@date 2024-01-15*/
    @Log("根据日期得到线性图表数据")
    @GetMapping(value = "/getLineChart")
    @AnonymousAccess
    public ResponseEntity<Object> getLineChart() {
        return  new ResponseEntity<>(moneyService.getLineChart(),HttpStatus.OK);
    }


}